<?php
	class ControllerExtensionModuleWater extends Controller{
		private $error = array();
		public function index(){
			$this->load->language('extension/module/water');
			$this->document->setTitle($this->language->get('heading_title'));
			$this->load->model('setting/setting');

			if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
				$this->model_setting_setting->editSetting('water', $this->request->post);
				$this->session->data['success'] = $this->language->get('text_success');
				$this->response->redirect($this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true));
			}

			$data['heading_title'] = $this->language->get('heading_title');
			$data['button_save'] = $this->language->get('button_save');
			$data['button_cancel'] = $this->language->get('button_cancel');
			$data['text_edit'] = $this->language->get('text_edit');
			$data['entry_water_type'] = $this->language->get('entry_water_type');
			$data['text_water_image'] = $this->language->get('text_water_image');
			$data['text_water_font'] = $this->language->get('text_water_font');
			$data['entry_water_position'] = $this->language->get('entry_water_position');
			$data['text_left_top'] = $this->language->get('text_left_top');
			$data['text_right_top'] = $this->language->get('text_right_top');
			$data['text_left_bottom'] = $this->language->get('text_left_bottom');
			$data['text_right_bottom'] = $this->language->get('text_right_bottom');
			$data['text_center'] = $this->language->get('text_center');
			$data['entry_water_font'] = $this->language->get('entry_water_font');
			$data['entry_water_alpha'] = $this->language->get('entry_water_alpha');
			$data['help_water_alpha'] = $this->language->get('help_water_alpha');
			$data['entry_water_image'] = $this->language->get('entry_water_image');
			$data['entry_status'] = $this->language->get('entry_status');
			$data['text_enabled'] = $this->language->get('text_enabled');
			$data['text_disabled'] = $this->language->get('text_disabled');
			$data['text_water_notice'] = $this->language->get('text_water_notice');

			$this->document->addScript('view/javascript/spectrum/spectrum.js');
			$this->document->addStyle('view/javascript/spectrum/spectrum.css');

			$data['breadcrumbs'] = array();
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('text_home'),
				'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], true)
			);
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('text_extension'),
				'href' => $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true)
			);
			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/water', 'token=' . $this->session->data['token'], true)
			);

			$data['action'] = $this->url->link('extension/module/water', 'token=' . $this->session->data['token'], true);
			$data['cancel'] = $this->url->link('extension/extension', 'token=' . $this->session->data['token'] . '&type=module', true);

			if (isset($this->request->post['water_type'])) {
				$data['water_type'] = $this->request->post['water_type'];
			} else {
				$data['water_type'] = $this->config->get('water_type');
			}

			if (isset($this->request->post['water_position'])) {
				$data['water_position'] = $this->request->post['water_position'];
			} else {
				$data['water_position'] = $this->config->get('water_position');
			}

			if (isset($this->request->post['water_font'])) {
				$data['water_font'] = $this->request->post['water_font'];
			} else {
				$data['water_font'] = $this->config->get('water_font');
			}

			if (isset($this->request->post['water_alpha'])) {
				$data['water_alpha'] = $this->request->post['water_alpha'];
			} else {
				$data['water_alpha'] = (int)$this->config->get('water_alpha');
			}

			if (isset($this->request->post['water_font_color'])) {
				$data['water_font_color'] = $this->request->post['water_font_color'];
			} else {
				$data['water_font_color'] = $this->config->get('water_font_color');
			}

			if (isset($this->request->post['water_status'])) {
				$data['water_status'] = $this->request->post['water_status'];
			} else {
				$data['water_status'] = $this->config->get('water_status');
			}

			$this->load->model('tool/image');

			if (isset($this->request->post['water_image']) && is_file(DIR_IMAGE . $this->request->post['water_image'])) {
				$data['water_image'] = $this->request->post['water_image'];
				$data['src_water_image'] = $this->model_tool_image->resize($this->request->post['water_image'], 100, 100);
			} else {
				$data['water_image'] = $this->config->get('water_image');
				if(is_file(DIR_IMAGE . $this->config->get('water_image'))){
					$data['src_water_image'] = $this->model_tool_image->resize($this->config->get('water_image'), 100, 100);
				}else{
					$data['src_water_image'] = $this->model_tool_image->resize('no_image.png', 100, 100);
				}
			}
			$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);

			if (isset($this->error['warning'])) {
				$data['error_warning'] = $this->error['warning'];
			} else {
				$data['error_warning'] = '';
			}

			$data['header'] = $this->load->controller('common/header');
			$data['column_left'] = $this->load->controller('common/column_left');
			$data['footer'] = $this->load->controller('common/footer');

			$this->response->setOutput($this->load->view('extension/module/water', $data));
		}
		protected function validate() {
			if (!$this->user->hasPermission('modify', 'extension/module/water')) {
				$this->error['warning'] = $this->language->get('error_permission');
			}

			return !$this->error;
		}
	}